package com.swpu.zdl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.swpu.zdl.mapper.PmsCategoryMapper;
import com.swpu.zdl.pojo.PmsCategory;
import com.swpu.zdl.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class OrderController {
   @Autowired
    private PmsCategoryMapper pmsCategoryMapper;
   @GetMapping("/get")
    public Result getall(){
       List<PmsCategory> pmsCategories = pmsCategoryMapper.selectList(new QueryWrapper<>());

       List<PmsCategory> collect = pmsCategories.stream()
               .filter(d -> d.getParentCid() == 0)
               .map(d -> {
                   d.setChildren(getChildren(d, pmsCategories));
                   return d;
               })
               .collect(Collectors.toList());


       return new Result(200, "", collect);
   }
   public List<PmsCategory> getChildren(PmsCategory root, List<PmsCategory> all){
       List<PmsCategory> collect = all.stream().filter(d -> d.getParentCid() == root.getCatId())
               .map(d -> {
                   d.setChildren(getChildren(d, all));
                   return d;
               }).collect(Collectors.toList());
       return collect;
   }
}
